<?php

namespace Frieza\Modules\McnUser\Controllers;

use App\Http\Controllers\Controller;
use Doraemon\model\McnUser;
use Doraemon\model\McnUserExtendInfo;
use Doraemon\model\McnUserProfession;
use Doraemon\tools\Log;
use Illuminate\Http\Request;

class McnUserSaveController extends Controller
{
    private $aMcnUserField = ['type', 'nick', 'age', 'level', 'user_production_level', 'fan_num', 'fan_viscosity', 'choiceness_dynamic_ratio', 'dynamic_total_num', 'choiceness_dynamic_total_num', 'choiceness_video_ratio', 'dynamic_exposure', 'dynamic_exposure_total_num', 'dynamic_interactive_total_num', 'totem_num', 'is_signing', 'lase_landing_time', 'last_production_time', 'house_area', 'house_style', 'house_pattern', 'brand_use', 'profession', 'people_num', 'pet', 'child', 'sex', 'area', 'address', 'carrying_capacity', 'user_active', 'is_designer', 'commerce_goods_label', 'is_auth', 'production_label', 'status', 'praise_collection', 'creator_level', 'business_coo_score', 'seven_note_num', 'register_time', 'login_place', 'house_place', 'love_category', 'seven_vistor_num', 'seven_vistor_frequency', 'seven_vistor_change', 'thirty_note_num', 'active_period', 'note_num', 'article_num', 'house_num', 'video_num', 'active_interaction_num', 'passive_interaction_num', 'hobby', 'skill', 'content_preference_short', 'content_preference_long', 'commodity_category_hobby', 'commodity_interest_preference', 'decoration_status', 'provider', 'location_house', 'house_ownership', 'register_place', 'special_family_member', 'cooperative_brand', 'circle', 'business_content_soft', 'business_content_hard'];
    private $aMcnUserExtendInfoField = ['cooperation_num', 'is_to_order', 'bus_dynamic_output', 'bus_video_output', 'bus_house_output', 'bus_article_output', 'control_ability', 'express_ability', 'lens_expression', 'industry_influence', 'cooperation_match', 'kol_label', 'redbook_fans', 'redbook_update', 'redbook_price', 'weibo_fans', 'weibo_update', 'weibo_price', 'image_text_price', 'video_one_minute_down_price', 'video_one_minute_up_price', 'stroll_id', 'performance_capacity', 'remark', 'description_home_style', 'user_profession', 'postal_address', 'whether_commerce_cooper', 'good_type', 'responsible_operation', 'community', 'operational_remarks', 'zhu_xiao_bang', 'yi_dou_tang', 'guang_guang', 'douyin', 'weibo', 'xiao_hong_shu', 'bilibili', 'zhihu', 'kuai_shou', 'other_platform', 'family_structure', 'personal_preference', 'human_description', 'description_home', 'fans_love_watch', 'is_sign', 'share_proportion', 'red_book_img_text_price', 'red_book_video_price', 'guang_guang_img_text_price', 'guang_guang_60s_video_price', 'guang_guang_60s_120s_video_price', 'is_label', 'is_super'];

    private const ESFIELDARR = [
        'sex',
        'user_profession',
        'whether_commerce_cooper',
        'other_platforms',
        'love_category',
        'family_structure',
        'personal_preference',
        'age',
        'fan_num',
        'praise_collection',
        'house_area',
        'business_coo_score',
        'uid',
        'nick',
        'login_place',
        'house_place',
        'level',
        'creator_level',
        'decoration_status',
        'special_family_member',
        'responsible_operation',
        'community',
        'human_description',
        'description_home',
        'area',
        'is_sign',
        'is_label',
        'is_super',
    ];

    public function index(Request $request, $iUid)
    {
        $aParam = $request->all();

        $aMcnUser = McnUser::get('*', ["uid" => $iUid]);
        $aMcnUserExt = McnUserExtendInfo::get('*', ["uid" => $iUid]);

        Log::debug("mcn接口请求数据", $aParam);

        $aMcnUserParam = [];
        $aMcnUserExtendInfoParam = [];

        foreach ($aParam as $sField => $value) {
            $sTableField = $sField;
            $tableValue = $value;

            $bJson = false;
            $aParamField = explode('JsonSep', $sField);
            if (count($aParamField) == 2) {
                $sTableField = $aParamField[0];
                $bJson = true;

                //家的形容-风格，特殊处理
                if ($sTableField == 'description_home' && strpos($aParamField[1], 'style') === 0) {
                    $aParamField[1] = 'style';
                }
            }

            if (in_array($sTableField, $this->aMcnUserField)) {
                if ($bJson) {
                    $aDbJson = json_decode($aMcnUser[$sTableField], true);
                    if (empty($aDbJson)) {
                        $aDbJson = [];
                    }
                    $aDbJson[$aParamField[1]] = $value;
                    $tableValue = json_encode($aDbJson, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
                    $aMcnUser[$sTableField] = $tableValue;
                }
                $aMcnUserParam[$sTableField] = $tableValue;
            }

            if (in_array($sTableField, $this->aMcnUserExtendInfoField)) {
                if ($bJson) {
                    $aDbJson = json_decode($aMcnUserExt[$sTableField], true);
                    if (empty($aDbJson)) {
                        $aDbJson = [];
                    }
                    $aDbJson[$aParamField[1]] = $value;
                    $tableValue = json_encode($aDbJson, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
                    $aMcnUserExt[$sTableField] = $tableValue;
                }
                $aMcnUserExtendInfoParam[$sTableField] = $tableValue;
            }

            if (in_array($sTableField, ['postal_address_1', 'postal_address_2'])) {
                //地址，特殊处理
                $aPostalAddress = explode('|', $aMcnUserExt['postal_address']);
                if ($sTableField == 'postal_address_1') {
                    $aPostalAddress[0] = $value;
                } else {
                    $aPostalAddress[1] = $value;
                }
                $aMcnUserExtendInfoParam['postal_address'] = implode('|', $aPostalAddress);
            }
        }

        if (!empty($aMcnUserParam)) {
            //更新数据
            Log::debug("更新mcn_user", $aMcnUserParam);
            McnUser::update($aMcnUserParam, ['uid' => $iUid]);

            //同步到es
            $aEsData = [];
            foreach ($aMcnUserParam as $k => $v) {
                if (in_array($k, self::ESFIELDARR)) {
                    $aEsData[$k] = $v;
                }
            }
            Log::debug('同步数据到es1', $aEsData);
            $client = new \Doraemon\model\Es\McnUser();
            $res = $client->saveData($iUid, $aEsData);
        }

        if (!empty($aMcnUserExtendInfoParam)) {
            //更新数据
            Log::debug("更新mcn_user_extend_info", [$aMcnUserExtendInfoParam, ['uid' => $iUid]]);
            McnUserExtendInfo::update($aMcnUserExtendInfoParam, ['uid' => $iUid]);

            //同步到es
            $aEsData = [];
            foreach ($aMcnUserExtendInfoParam as $k => $v) {
                if (in_array($k, self::ESFIELDARR)) {

                    //职业给es传name
                    if ($k == 'user_profession') {
                        $aProfession = McnUserProfession::get('*', ['id' => $v]);
                        if ($aProfession) {
                            $v = $aProfession['name'];
                        } else {
                            $v = '';
                        }
                    }

                    $aEsData[$k] = $v;
                }
            }
            Log::debug('同步数据到es2', $aEsData);
            $client = new \Doraemon\model\Es\McnUser();
            $res = $client->saveData($iUid, $aEsData);
        }

        return 'success';
    }
}
